package net.muchine.core.support;

import java.io.IOException;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

@Component
public class Codes {
	private Map<String, String> timeZoneList = new LinkedHashMap<String, String>();
	private Map<String, String> languageList = new LinkedHashMap<String, String>();
	
	public Codes() {
		this.initTimeZoneList();
		this.initLanguageList();
	}
	
	private void initTimeZoneList() {
		timeZoneList.put("", "");
		this.initPropertyList("/properties/timezone.properties", timeZoneList);
	}
	
	private void initLanguageList() {
		languageList.put("", "(auto)");
		this.initPropertyList("/properties/language.properties", languageList);
	}
	
	private void initPropertyList(String resourceLocation, Map<String, String> list) {
		LinkedProperties prop = new LinkedProperties();
		
		try {
			prop.load(Codes.class.getResourceAsStream(resourceLocation));
		} catch (IOException e) {
			throw new IllegalStateException(
				"Properties cannot be initialized. Location: " + resourceLocation, e);
		}
		
		Enumeration<Object> keys = prop.keys();
		while (keys.hasMoreElements()) {
			String key = (String) keys.nextElement();
			list.put(key, prop.getProperty(key));
		}
	}
	
	public Map<String, String> getTimeZoneList() {
		return this.timeZoneList;
	}
	
	public Map<String, String> getLanguageList() {
		return this.languageList;
	}
}
